<template>
  <span v-auth>
    <el-dropdown
      @command="onCommandClick($event)"
      :show-timeout="100"
    >
      <el-button
        v-if="!link"
        type="primary"
        plain
        split-button
      >
        更多<i class="el-icon-arrow-down el-icon--right"></i>
      </el-button>
      <span v-else class="el-dropdown-link">
          更多<i class="el-icon-arrow-down el-icon--right"></i>
      </span>
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item
          v-auth="'finance/wallet'"
          command="wallet"
          icon="el-icon-wallet">钱包</el-dropdown-item>
        <el-dropdown-item
          v-auth="'user-health-archive'"
          command="health-archive"
          icon="el-icon-document"
          divided>健康档案</el-dropdown-item>
        <el-dropdown-item
          v-auth="'user-more/user-returnvisit'"
          command="returnvisit"
          icon="el-icon-document">回访记录</el-dropdown-item>
        <el-dropdown-item
          v-auth="'user-more/user-address'"
          command="address"
          icon="el-icon-goods">收货地址</el-dropdown-item>
        <template v-if="user.userType == 2">
          <el-dropdown-item
            v-auth="'user_vipcard/changeCard'"
            command="vipcard-change"
            icon="el-icon-bank-card"
            divided>更换卡</el-dropdown-item>
          <el-dropdown-item
            v-auth="'user_vipcard/user_vipcard_changeRecord'"
            command="vipcard-change-record"
            icon="el-icon-bank-card">卡更换记录</el-dropdown-item>
        </template>
        <template v-if="user.userType == 9">
          <el-dropdown-item
            v-auth="'device/device-detail'"
            command="hubei-settings"
            icon="el-icon-setting"
            divided>设备设置</el-dropdown-item>
          <el-dropdown-item
            v-auth="'device/device-changeDeviceCode'"
            command="hubei-change-device-code"
            icon="el-icon-bank-card">更换设备号</el-dropdown-item>
        </template>
        <el-dropdown-item
          v-auth="'device-setValid'"
          command="set-validity"
          icon="el-icon-refresh"
          divided>启用/禁用</el-dropdown-item>
        <el-dropdown-item
          v-if="user.userType <= 1"
          v-auth="'user-resetPwd'"
          command="user-reset-password"
          icon="el-icon-key"
          divided >重置用户密码</el-dropdown-item>
        <el-dropdown-item
          v-auth="'user.update'"
          command="user-edit"
          icon="el-icon-edit"
          divided >修改</el-dropdown-item>
        <el-dropdown-item
          v-auth="'user-cancel'"
          command="user-cancel"
          icon="el-icon-document-delete"
          divided >注销用户</el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>

    <vip-card-change ref="vipCardChange"></vip-card-change>
  </span>
</template>

<script>
export default {
  components: {
    VipCardChange: () => import('./vipcard/change/Change.vue')
  },
  props: {
    user: Object,
    openTab: Function,
    table: Function,
    link: Boolean
  },
  data: () => ({permission: '/smart_info_center/life/user/user'}),
  methods: {
    command(cmd, user) {
      switch (cmd) {
        case 'user-edit':
          this.pushPage({
            path: '/user/register',
            key: user.id,
            params: {
              mode: 'update',
              user,
              onSuccess: () => {
                this.table && this.table().reloadCurrentPage();
              }
            }
          });
          break;
        case 'health-archive':
          this.openTab({
            url: 'view/health/archive/index.do?userId=' + user.id,
            title: '健康档案'
          });
          break;
        case 'returnvisit':
          this.openTab({
            url: 'view/community/returnvisit/index.do?userId=' + user.id,
            title: (user.realName || user.aliasName)  + ' - 回访记录'
          });
          break;
        case 'address':
          this.openTab({
            url: "view/user/address/index.do?userId=" + user.userId + '&_func_code=user-address',
            title: (user.realName || user.aliasName) + " - 收货地址"
          });
          break;
        case 'wallet':
          this.pushPage({
            path: '/user/wallet',
            params: { user },
            subTitle: user.realName || user.aliasName,
            key: user.id
          });
          break;
        case 'set-validity':
          const act = user.isvalid ? '禁用' : '启用';
          this.$confirm(`${user.isvalid ? '' : '当前为禁用状态，'}确认${act}？`, `确认${act}`, {
            type: 'warning'
          }).then(async () => {
            const actVal = +!user.isvalid;
            const ret = await this.$http.post('/api/device/setValid',
              {deviceId: user.id, isvalid: actVal});
            if (ret.success) {
              this.$message.success(act + '成功');
              user.isvalid = actVal;
              this.$pageEventBus.emit(this, 'user-update', user.id);
            }
          });
          break;
        case 'user-cancel':
          this.$confirm(`确认注销用户 ${user.realName || user.aliasName}？`, `确认`, {
            type: 'warning'
          }).then(async () => {
            const ret = await this.$http.post('/api/user/cancelUser',
                {userId: user.id, userType: user.userType});
            if (ret.success) {
              this.$message.success('注销用户成功');
              this.table && this.table().reloadCurrentPage();
              this.$pageEventBus.emit(this, 'user-delete', user);
            } else {
              this.$message.error(ret.message || '操作失败');
            }
          });
          break;
        case 'user-reset-password':
          this.$confirm(`确认重置用户 ${user.realName || user.aliasName} 的密码 ？`, `确认`, {
            type: 'warning'
          }).then(async () => {
            const ret = await this.$http.post('/api/user/resetPassword', {userId: user.id});
            if (ret.success) {
              this.$message.success('重置密码成功');
            }
          });
          break;
        case 'vipcard-change':
          this.$refs.vipCardChange.show({
            user,
            onSuccess: ({newCardCode}) => {
              user.deviceCode = newCardCode;
              this.$pageEventBus.emit(this, 'user-update', user.id);
            }
          });
          break;
        case 'vipcard-change-record':
          this.openTab({
            url: 'view/user/vipcard/change/record.do?cardCode=' + user.deviceCode,
            title: '会员卡更换记录'
          });
          break;
        case 'hubei-settings':
          this.openTab({
            url: 'device/showDeviceDetail.do?_func_code=device-detail&deviceId=' + user.id,
            title: '设备设置'
          });
          break;
        case 'hubei-change-device-code':
          this.$prompt(`${user.realName || user.aliasName} - 更换设备号`, {
            inputPlaceholder: '请输入新的设备号',
            inputPattern: /^\d+$/
          }).then(async ({action, value}) => {
            if ('confirm' == action) {
              const ret = await this.$http.post('/api/device/changeDeviceCode',
                {deviceCode: value, oldDeviceId: user.id, oldDeviceCode: user.deviceCode});
              if (ret.success) {
                this.$message.success('更换设备号成功');
                this.$pageEventBus.emit(this, 'user-update', user.id);
                this.table && this.table().reloadCurrentPage();
              }
            }
          });
          break;
      }
    },
    onCommandClick(cmd) {
      this.command(cmd, this.user);
    }
  }
}
</script>

<style scoped>
>>> .el-dropdown-link {
  cursor: pointer;
  color: #409EFF;
}
</style>
